package com.manger.thread;

import com.manger.format.CaseFilesFormat;
import com.manger.model.Project;
import com.manger.utils.DeCompressUtils;
import com.manger.utils.DetNameEnum;
import com.manger.utils.ProjectUtils;
import com.manger.utils.SystemConstant;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class CaseFilesFormatThread extends Thread {

    private static final Logger log = LoggerFactory.getLogger(CaseFilesFormatThread.class);
    private String targetDirPath;

    private String resourceDirPath;

    public CaseFilesFormatThread(String targetDirPath,String resourceDirPath) {
        this.targetDirPath = targetDirPath;
        this.resourceDirPath = resourceDirPath;
    }

    @Override
    public void run() {
       /* File zipFile = new File(zipFilePath);
        if (!zipFile.exists()) {
            return;
        }
        String dirName = zipFile.getName().substring(0,zipFile.getName().lastIndexOf("."));
        DetNameEnum detNameEnum = DetNameEnum.geDetNameEnumByDesc(dirName);
        if (detNameEnum == null) {
            System.out.println("请检查压缩包名称是否正确!");
            return;
        }

        String extractDir = zipFile.getParentFile().getAbsolutePath() + File.separator + "format" + File.separator + System.currentTimeMillis() + "/" + dirName;
        File extractDirFile = new File(extractDir);
        extractDirFile.mkdirs();
        try {
            DeCompressUtils.unZip(zipFile.getAbsolutePath(), extractDir);
            System.out.println("解压成功：【" + zipFile.getCanonicalPath() + "】");
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("解压压缩包失败，请检查是否是标准zip文件！");
            return;
        }*/

        try {
            File resourceFile = new File(resourceDirPath);
            if(!resourceFile.exists()){
                System.out.println("没有检测到文件夹!");
                return;
            }
            String dirName = resourceFile.getName();
            DetNameEnum detNameEnum = DetNameEnum.geDetNameEnumByDesc(dirName.toLowerCase());
            if (detNameEnum == null) {
                System.out.println("请检查文件夹名称是否正确!");
                return;
            }
            Project project = ProjectUtils.projectMap.get(detNameEnum);
            Class<? extends CaseFilesFormat> clazz = detNameEnum.getClazz();
            Object object = clazz.newInstance();
            System.out.println("+++++++++++++++++++++++++++++++++++++++++++++");
            System.out.println("开始整理：【"+project.getDebtName()+"】");
            System.out.println("材料目录: " + resourceDirPath);
            System.out.println("结果目录："+ targetDirPath);
            ((CaseFilesFormat) object).format(resourceDirPath, targetDirPath, project);
            System.out.println("【整理结束】");
        }catch (Exception e){
            System.out.println("整理数据失败！");
            e.printStackTrace();
        }
        System.out.println("+++++++++++++++++++++++++++++++++++++++++++++");
    }
}
